package com.yinwq.shorturl.service.impl;

import com.google.common.collect.Lists;
import com.yinwq.shorturl.service.ReplaceSqlParamsService;
import com.yinwq.shorturl.utils.DateUtil;
import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author: yinwenqiang@qccr.com
 * @date: Created in 2020/8/12 11:28
 */


@Service
public class ReplaceSqlParamsServiceImpl implements ReplaceSqlParamsService {

    enum SQLParamTypeEnum {
        Integer,
        Timestamp
    }

    @Override
    public String replaceSqlParams(String sql, String sqlParamsStr) {
        if (StringUtils.isEmpty(sqlParamsStr) || StringUtils.isEmpty(sql)) {
            return sql;
        }

        List<Object> params = Lists.newArrayList(sqlParamsStr.split(", "));
        params = params.stream().map(param -> {
            String[] paramInfo = param.toString().split("\\(");
            if (paramInfo[1].contains(SQLParamTypeEnum.Integer.name())) {
                return param = Integer.valueOf(paramInfo[0]);
            } else if (paramInfo[1].contains(SQLParamTypeEnum.Timestamp.name())) {
                DateTimeFormatter dateTimeFormat = DateTimeFormat.forPattern(DateUtil.LONG_DATE_TIME_PATTERN);
                return dateTimeFormat.parseDateTime(paramInfo[0]).toString(DateUtil.LONG_DATE_TIME_PATTERN);
                //return param = new DateTime(paramInfo[0]).toDate();
            } else {
                return paramInfo[0];
            }
        }).collect(Collectors.toList());

        StringBuffer returnSQL = new StringBuffer();
        String[] subSQL = sql.split("\\?");
        for (int i = 0; i < params.size(); i++) {
            if (params.get(i) instanceof Integer) {
                returnSQL.append(subSQL[i]).append(params.get(i));
            } else {
                returnSQL.append(subSQL[i]).append("'").append(params.get(i))
                        .append("'");
            }
        }
        if (subSQL.length > params.size()){
            returnSQL.append(subSQL[params.size()]);
        }
        return returnSQL.toString();
    }

    public static void main(String[] args) {
        String sql = "select id, task_setting_id, task_name, task_type_code, store_id, car_id, plate_number, client_name, client_phone, trigger_time, delay_time, complete_time, terminate_time, fail_time, terminate_reason, remark, receive_user_id, biz_id, work_order_id, sub_biz_id, business_id, biz_time, status, create_time, create_person, update_time, update_person,group_auxiliary_field from ds_task where 1=1 and status in ( ? , ? ) and plate_number = ? and store_id = ? and task_type_code = ?";
        String sqlParamsStr = "1(Integer), 6(Integer), 苏EW15U2(String), 1626271(Long), sj_keep(String)";
        ReplaceSqlParamsServiceImpl replaceSqlParamsService = new ReplaceSqlParamsServiceImpl();
        System.err.println(replaceSqlParamsService.replaceSqlParams(sql, sqlParamsStr));
    }
}
